﻿using Avalonia;
using Avalonia.Controls;
using FantomNext.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FantomNext.Presenters
{
    // SelectionManage
    /// <summary>
    /// 选框管理
    /// </summary>
    public partial class ScenePresenter
    {
        #region defines

        /// <summary>
        /// 用于存储当前选框配对信息的结构体。
        /// </summary>
        /// <param name="SelectionFrame"></param>
        /// <param name="ShapePresenter"></param>
        private record SelectionFrameShapePresenterPair(SelectionFrame SelectionFrame, ShapePresenter ShapePresenter);

        #endregion

        #region fields

        /// <summary>
        /// 用于存储当前选框配对信息的集合。
        /// </summary>
        private List<SelectionFrameShapePresenterPair> _selectionFrames = new List<SelectionFrameShapePresenterPair>();

        #endregion

        #region events impls

        private void SelectionFrame_SelectionFrameSizeChanged(object? sender, Rect e)
        {
            var selectionFrame = sender as SelectionFrame;
            if (selectionFrame is null) return;
            var selectionFrameShapePresenterPair = GetSelectionFrameShapePresenterPair(selectionFrame);
            var shapePresenter = selectionFrameShapePresenterPair.ShapePresenter;


            var uiLeftTopPos = selectionFrame.TranslatePoint(e.TopLeft, View)!.Value;
            var uiRightBottomPos = selectionFrame.TranslatePoint(e.BottomRight, View)!.Value;


            var sceneLeftTopPos = ConvertVisualPointToScenePoint(uiLeftTopPos);
            var sceneRightBottomPos = ConvertVisualPointToScenePoint(uiRightBottomPos);

            var width = sceneRightBottomPos.X - sceneLeftTopPos.X;
            var height = sceneRightBottomPos.Y - sceneLeftTopPos.Y;


            Debug.WriteLine($"SelectionFrame_SelectionFrameSizeChanged {width} {height} {sceneLeftTopPos} {sceneRightBottomPos}");

            shapePresenter.ScenePosition = new Shared.Vector2(sceneLeftTopPos.X, sceneLeftTopPos.Y);
            shapePresenter.SceneSize = new Shared.Vector2(width,height);
        }

        #endregion

        #region methods

        /// <summary>
        /// 坐标参考是画布坐标，为此需要进行一层科学转换，以保证能够贴合。
        /// </summary>
        private void CreateSelection(ShapePresenter shapePresenter)
        {
            var view = _view;
            var selectionCanvas = _view.PART_SelectionCanvas;

            var selectionFrame = new SelectionFrame();
            selectionFrame.SelectionFrameSizeChanged += SelectionFrame_SelectionFrameSizeChanged;
            var selectionFrameShapePresenter = new SelectionFrameShapePresenterPair(selectionFrame, shapePresenter);

            // 调整到正确的尺寸。
            VisualizeSelectionFrame(selectionFrameShapePresenter);

            // 添加当前选框。
            _selectionFrames.Add(selectionFrameShapePresenter);
            selectionCanvas.Children.Add(selectionFrame);
        }


        public void SetSelection(IEnumerable<string> shapeIdList)
        {
            if (shapeIdList is null) return;

            // 当前选中项。
            var oldIdList = _selectionFrames.Select(i => i.ShapePresenter.Id);

            // 传入项但是排除当前选中项=>就是新增加的项。
            var newIdList = shapeIdList.Except(oldIdList);
            var newShapePresenters = _shapes.Where(s => newIdList.Contains(s.Id));

            // 批量创建吧。
            foreach (var newItem in newShapePresenters)
            {
                CreateSelection(newItem);
            }
        }

        /// <summary>
        /// 清除当前的选框。
        /// </summary>
        public void ClearSelection()
        {
            var canvas = _view.PART_SelectionCanvas;
            canvas.Children.Clear();
            _selectionFrames.Clear();
        }

        private SelectionFrameShapePresenterPair GetSelectionFrameShapePresenterPair(ShapePresenter shapePresenter)
        {
            return _selectionFrames.FirstOrDefault(i => i.ShapePresenter == shapePresenter);
        }

        private SelectionFrameShapePresenterPair GetSelectionFrameShapePresenterPair(SelectionFrame selectionFrame)
        {
            return _selectionFrames.FirstOrDefault(i => i.SelectionFrame == selectionFrame);
        }

        /// <summary>
        /// 选框可视化调整。
        /// </summary>
        /// <param name="pair"></param>
        private void VisualizeSelectionFrame(SelectionFrameShapePresenterPair pair)
        {
            if (pair is null) return;
            var selectionFrame = pair.SelectionFrame;
            var shapePresenter = pair.ShapePresenter;

            var transformRect = shapePresenter.GetVisualRect(this);

            Canvas.SetLeft(selectionFrame, transformRect.Left);
            Canvas.SetTop(selectionFrame, transformRect.Top);
            selectionFrame.Width = transformRect.Width;
            selectionFrame.Height = transformRect.Height;
        }

        #endregion
    }
}
